package com.zzl.services.impl;

import com.zzl.entity.Grade;
import com.zzl.entity.PageBean;
import com.zzl.services.GradeServices;
import com.zzl.util.StringEmptyUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GradeServicesImpl implements GradeServices {
    @Override
    public int gradeaCount(Connection con, Grade grade) throws SQLException {
        StringBuffer sb=new StringBuffer("select count(*) as total from t_grade");
        if(StringEmptyUtil.isNotEmpty(grade.getGradeName())){
            sb.append(" and gradeName like '%"+grade.getGradeName()+"%'");
        }
        PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and","where"));
        ResultSet rs=pstmt.executeQuery();
        if (rs.next()){
            return rs.getInt("total");
        } else {
            return 0;
        }
    }

    @Override
    public ResultSet gradeList(Connection con, PageBean pageBean, Grade grade) throws SQLException {
        StringBuffer sb=new StringBuffer("select * from t_grade");
        if(grade!=null&&StringEmptyUtil.isNotEmpty(grade.getGradeName())){
            sb.append(" and gradeName like '%"+grade.getGradeName()+"%'");
        }
        if (pageBean!=null){
            sb.append(" limit "+pageBean.getStart()+","+pageBean.getRows());
        }
        PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and","where"));
        return pstmt.executeQuery();
    }

    @Override
    public int gradeDelete(Connection con, String delIds) throws SQLException {
        String sql="delete from t_grade where id in (" + delIds + ")";
        PreparedStatement pstmt=con.prepareStatement(sql);
        return pstmt.executeUpdate();
    }

    @Override
    public int gradeAdd(Connection con, Grade grade) throws SQLException {
        String sql="insert into t_grade values(null,?,?)";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1,grade.getGradeName());
        pstmt.setString(2,grade.getGradeDesc());
        return pstmt.executeUpdate();
    }

    @Override
    public int gradeModify(Connection con, Grade grade) throws SQLException {
        String sql="update t_grade set gradeName=?,gradeDesc=? where id=?";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1,grade.getGradeName());
        pstmt.setString(2,grade.getGradeDesc());
        pstmt.setInt(3,grade.getId());
        return pstmt.executeUpdate();
    }

    @Override
    public boolean hasStudentsInGrade(Connection con, String gradeId) {
        // 实现判断班级下是否有学生的逻辑，示例 SQL 查询
        String sql = "SELECT COUNT(*) FROM t_student WHERE gradeId = ?";
        try (PreparedStatement pstmt = con.prepareStatement(sql)) {
            pstmt.setString(1, gradeId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    int count = rs.getInt(1);
                    return count > 0;
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

}
